package com.ds.esd.bpm.custom.action;

import com.ds.bpm.client.*;
import com.ds.bpm.engine.BPMException;
import com.ds.bpm.engine.WorkflowClientService;
import com.ds.bpm.engine.query.BPMCondition;
import com.ds.bpm.engine.query.BPMConditionKey;
import com.ds.bpm.enums.right.RightConditionEnums;
import com.ds.common.JDSException;
import com.ds.common.query.Operator;
import com.ds.common.query.Order;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ListResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.context.JDSActionContext;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.bpm.custom.perform.FormTabs;
import com.ds.esd.bpm.worklist.view.ActivityInstListView;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.RequestPathEnum;
import com.ds.esd.custom.api.enums.ResponsePathEnum;
import com.ds.esd.custom.module.annotation.DynLoadAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavTabsViewAnnotation;
import com.ds.esd.custom.toolbar.bpm.RouteCustomMenu;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.repository.database.proxy.DSMTableProxy;
import com.ds.esd.util.TabPageUtil;
import com.ds.jds.core.esb.EsbUtil;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(path = "/bpm/processdef/")
@MethodChinaName(cname = "流程定义", imageClass = "bpmfont bpmgongzuoliuzhutiguizeweihuguanli")
public class ProcessDefRowAction {


    @RouteCustomMenu(routeType = {RouteToType.Reload})
    @NavTabsViewAnnotation()
    @DynLoadAnnotation
    @RequestMapping(method = RequestMethod.POST, value = "NewProcess")
    @APIEventAnnotation(customRequestData = {RequestPathEnum.treegridrow, RequestPathEnum.ctx}, customResponseData = ResponsePathEnum.form)
    @DialogAnnotation
    @ModuleAnnotation(dynLoad = true, imageClass = "bpmfont bpmgongzuoliuzhutiguizeweihuguanli", caption = "新建流程", name = "table")
    public @ResponseBody
    TreeListResultModel<List<FormTabs>> newProcess(String processDefVersionId, String activityInstId, String processInstId) {

        TreeListResultModel<List<FormTabs>> formTabsTree = new TreeListResultModel<>();
        List<DSMTableProxy> tableProxyList = new ArrayList<>();
        try {
            if (processDefVersionId != null) {
                ProcessDefVersion version = getClient().getProcessDefVersion(processDefVersionId);
                ProcessInst processInst = getClient().newProcess(version.getProcessDefId(), null, null, null);
                ActivityInst activityInst = processInst.getActivityInstList().get(0);
                JDSActionContext.getActionContext().getContext().put("activityInstId", activityInst.getActivityInstId());
                ProcessDefVersion processDefVersion = activityInst.getProcessDefVersion();
                String domainId = processDefVersion.getAttribute("domainId");
                ProcessDefForm formDef = processDefVersion.getFormDef();

                List<String> tableNames = formDef.getTableNames();
                for (String tableName : tableNames) {
                    DSMTableProxy proxy = DSMFactory.getInstance().getRepositoryManager().getTableProxyByName(tableName, domainId);
                    tableProxyList.add(proxy);
                }
                formTabsTree = TabPageUtil.getTabList(tableProxyList, FormTabs.class);
                formTabsTree.addCtx("activityInstId", activityInst.getActivityInstId());
                formTabsTree.addCtx("processInstId", activityInst.getProcessInstId());
            }

        } catch (BPMException e) {
            e.printStackTrace();
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return formTabsTree;
    }


    @MethodChinaName(cname = "监控")
    @RequestMapping(method = RequestMethod.POST, value = "processAdmin")
    @GridViewAnnotation()
    @ModuleAnnotation(imageClass = "bpmfont bpmgongzuoliujiankong", caption = "监控")
    @APIEventAnnotation(customRequestData = RequestPathEnum.treegridrow)
    public @ResponseBody
    ListResultModel<List<ActivityInstListView>> getMyActivityInstList(String processDefVersionId) {
        ListResultModel<List<ActivityInstListView>> result = new ListResultModel<List<ActivityInstListView>>();
        try {
            ProcessDefVersion version = getClient().getProcessDefVersion(processDefVersionId);
            BPMCondition condition = new BPMCondition(BPMConditionKey.PROCESSDEF_ID,
                    Operator.EQUALS, version.getProcessDefId());
            condition.addOrderBy(new Order(BPMConditionKey.ACTIVITYINST_ARRIVEDTIME, false));
            ListResultModel<List<ActivityInst>> activityInstList = getClient().getActivityInstList(condition, RightConditionEnums.CONDITION_ALLWORK, null, null);
            result = PageUtil.changPageList(activityInstList, ActivityInstListView.class);
        } catch (BPMException e) {
            result = new ErrorListResultModel();
            ((ErrorListResultModel<List<ActivityInstListView>>) result).setErrdes(e.getMessage());
            ((ErrorListResultModel<List<ActivityInstListView>>) result).setErrcode(e.getErrorCode());
            e.printStackTrace();
        }

        return result;

    }


    /**
     * @return
     */
    public WorkflowClientService getClient() {

        WorkflowClientService client = ((WorkflowClientService) EsbUtil.parExpression("$BPMC"));

        return client;
    }
}

